package com.bing.bingPro.controller;

import com.bing.bingPro.ExcelUtil;
import com.bing.bingPro.common.BaseResult;
import com.bing.bingPro.controller.dto.TOrderMissReportDTO;
import com.bing.bingPro.controller.dto.TOrderMissReportFromsDTO;
import com.bing.bingPro.controller.dto.TOrderMonthDto;
import com.bing.bingPro.controller.dto.TOrderYearDto;
import com.bing.bingPro.controller.vo.*;
import com.bing.bingPro.service.TOrderIndexBusiMonthService;
import com.bing.bingPro.service.TOrderMissMonthService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
public class GongdanController {

    @Resource
    private TOrderMissMonthService tOrderMissMonthService;

    @Resource
    private TOrderIndexBusiMonthService tOrderIndexBusiMonthService;


    @PostMapping("/trend")
    public BaseResult<List<DataMissDtoVo>> queryDataTrend(@RequestBody TOrderYearDto tOrderYearDto) {
        return new BaseResult<>(this.tOrderMissMonthService.queryDataTrend(tOrderYearDto));
    }

    @PostMapping("/getTOrderMissReportFromsInfo")
    public BaseResult<List<TOrderMissReportFromsVO>> getTOrderMissReportFromsInfo(@RequestBody TOrderMissReportFromsDTO tOrderMissReportFromsDTO) {
        tOrderMissReportFromsDTO.setProvinceName(tOrderMissReportFromsDTO.getProvinceName());
        return new BaseResult<>(this.tOrderMissMonthService.getTOrderMissReportFromsInfo(tOrderMissReportFromsDTO));
    }


    @PostMapping("/allSort")
    public BaseResult<List<TOrderMissReportVo>> queryDataSort(@RequestBody TOrderMissReportDTO tOrderMissMonthDto) {
        if ((tOrderMissMonthDto.getTimeGranularity()) == null) {
            return new BaseResult<>(404, "参数有误，timeGranularity(时间粒度)是必填参数！");
        }

        return new BaseResult<>(this.tOrderMissMonthService.queryDataSort(tOrderMissMonthDto));
    }


   /* @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody TOrderMissReportFromsDTO tOrderMissReportFromsDTO) throws Exception {

        List<TOrderMissReportFromsVO> tOrderMissReportFromsInfo = this.tOrderMissMonthService.getTOrderMissReportFromsInfo(tOrderMissReportFromsDTO);

        //excel 数据集合
        List<TOrderMissExportDTO> tOrderMissExportDTOS = new ArrayList<>();
        tOrderMissReportFromsInfo.forEach(tOrderMissReportFromsVO -> {
            TOrderMissExportDTO tOrderMissExportDTO = new TOrderMissExportDTO();
            tOrderMissExportDTO.setProvinceName(tOrderMissReportFromsVO.getProvinceName());

            tOrderMissExportDTO.setMoveCount("0");
            tOrderMissExportDTO.setMoveOrderMissRate("0%");
            tOrderMissExportDTO.setMoveDateList("无");

//cp/投诉工单、install/装机工单、move/移机工单、detach/拆机工单、lstf/低满工单、cp_instl_move_detach/四类工单总计(除低满工单)
            tOrderMissExportDTO.setCpCount("0");
            tOrderMissExportDTO.setCpOrderMissRate("0%");
            tOrderMissExportDTO.setCpDateList("无");

            tOrderMissExportDTO.setDetachCount("0");
            tOrderMissExportDTO.setDetachOrderMissRate("0%");
            tOrderMissExportDTO.setDetachDateList("无");

            tOrderMissExportDTO.setInstallCount("0");
            tOrderMissExportDTO.setInstallOrderMissRate("0%");
            tOrderMissExportDTO.setInstallDateList("无");

            tOrderMissExportDTO.setLstfCount("0");
            tOrderMissExportDTO.setLstfOrderMissRate("0%");
            tOrderMissExportDTO.setLstfDateList("无");

            tOrderMissExportDTO.setCpInstlMoveDetachCount("0");
            tOrderMissExportDTO.setCpInstlMoveDetachOrderMissRate("0%");
            tOrderMissExportDTO.setCpInstlMoveDetachDateList("无");


            tOrderMissExportDTO.setProvinceName(tOrderMissReportFromsVO.getProvinceName());
            //获取工单类型集合
            List<TOrderMissDetailsDTO> tOrderMissDetailsDTOList = tOrderMissReportFromsVO.getTOrderMissDetailsDTOList();
            if (CollUtil.isNotEmpty(tOrderMissDetailsDTOList)) {
                //根据工单类型集合分组
                Map<String, List<TOrderMissDetailsDTO>> collect = tOrderMissDetailsDTOList.stream().collect(Collectors.groupingBy(TOrderMissDetailsDTO::getOrderType));
                //所有工单类型
                List<String> orderTypeAllList = this.tOrderMissMonthService.list().stream().map(TOrderMissMonth::getOrderType).distinct().collect(Collectors.toList());


                collect.forEach((orderType, tOrderMissDetailsDTOListMap) -> {
                    if ("move".equals(orderType)) {
                        orderTypeAllList.remove(orderType);
                        collect.get(orderType).forEach(tOrderMissDetailsDTO -> {
                            String str = tOrderMissDetailsDTO.getOrderMissDateList().toString();
                            orderTypeAllList.remove(tOrderMissDetailsDTO.getOrderType());
                            tOrderMissExportDTO.setMoveOrderMissRate(NumberUtil.decimalFormat("#.##%", tOrderMissDetailsDTO.getOrderMissRate()));
                            tOrderMissExportDTO.setMoveCount(tOrderMissDetailsDTO.getCount().toString());
                            tOrderMissExportDTO.setMoveDateList(str.substring(1, str.length() - 1));
                        });
                    }

                    if ("cp_instl_move_detach".equals(orderType)) {
                        orderTypeAllList.remove(orderType);
                        collect.get(orderType).forEach(tOrderMissDetailsDTO -> {
                            String str = tOrderMissDetailsDTO.getOrderMissDateList().toString();
                            orderTypeAllList.remove(tOrderMissDetailsDTO.getOrderType());
                            tOrderMissExportDTO.setCpInstlMoveDetachOrderMissRate(NumberUtil.decimalFormat("#.##%", tOrderMissDetailsDTO.getOrderMissRate()));
                            tOrderMissExportDTO.setCpInstlMoveDetachCount(tOrderMissDetailsDTO.getCount().toString());
                            tOrderMissExportDTO.setCpInstlMoveDetachDateList(str.substring(1, str.length() - 1));
                        });
                    }

                    if ("detach".equals(orderType)) {
                        orderTypeAllList.remove(orderType);
                        collect.get(orderType).forEach(tOrderMissDetailsDTO -> {
                            String str = tOrderMissDetailsDTO.getOrderMissDateList().toString();
                            orderTypeAllList.remove(tOrderMissDetailsDTO.getOrderType());
                            tOrderMissExportDTO.setDetachOrderMissRate(NumberUtil.decimalFormat("#.##%", tOrderMissDetailsDTO.getOrderMissRate()));
                            tOrderMissExportDTO.setDetachCount(tOrderMissDetailsDTO.getCount().toString());
                            tOrderMissExportDTO.setDetachDateList(str.substring(1, str.length() - 1));
                        });
                    }

                    if ("install".equals(orderType)) {
                        orderTypeAllList.remove(orderType);
                        collect.get(orderType).forEach(tOrderMissDetailsDTO -> {
                            String str = tOrderMissDetailsDTO.getOrderMissDateList().toString();
                            orderTypeAllList.remove(tOrderMissDetailsDTO.getOrderType());
                            tOrderMissExportDTO.setInstallOrderMissRate(NumberUtil.decimalFormat("#.##%", tOrderMissDetailsDTO.getOrderMissRate()));
                            tOrderMissExportDTO.setInstallCount(tOrderMissDetailsDTO.getCount().toString());
                            tOrderMissExportDTO.setInstallDateList(str.substring(1, str.length() - 1));
                        });
                    }
//cp/投诉工单、install/装机工单、move/移机工单、detach/拆机工单、lstf/低满工单、cp_instl_move_detach/四类工单总计(除低满工单)
                    if ("cp".equals(orderType)) {
                        orderTypeAllList.remove(orderType);
                        collect.get(orderType).forEach(tOrderMissDetailsDTO -> {
                            String str = tOrderMissDetailsDTO.getOrderMissDateList().toString();
                            orderTypeAllList.remove(tOrderMissDetailsDTO.getOrderType());
                            tOrderMissExportDTO.setCpOrderMissRate(NumberUtil.decimalFormat("#.##%", tOrderMissDetailsDTO.getOrderMissRate()));
                            tOrderMissExportDTO.setCpCount(tOrderMissDetailsDTO.getCount().toString());
                            tOrderMissExportDTO.setCpDateList(str.substring(1, str.length() - 1));
                        });
                    }

                    if ("lstf".equals(orderType)) {
                        orderTypeAllList.remove(orderType);
                        collect.get(orderType).forEach(tOrderMissDetailsDTO -> {
                            String str = tOrderMissDetailsDTO.getOrderMissDateList().toString();
                            orderTypeAllList.remove(tOrderMissDetailsDTO.getOrderType());
                            tOrderMissExportDTO.setLstfCount(tOrderMissDetailsDTO.getCount().toString());
                            tOrderMissExportDTO.setLstfOrderMissRate(NumberUtil.decimalFormat("#.##%", tOrderMissDetailsDTO.getOrderMissRate()));
                            tOrderMissExportDTO.setLstfDateList(str.substring(1, str.length() - 1));
                        });
                    }


                });
            }
            tOrderMissExportDTOS.add(tOrderMissExportDTO);
        });

        final String fileName = "用户信息";
        ExcelUtil.writeExcel(response, tOrderMissExportDTOS, fileName, "用户信息", TOrderMissExportDTO.class);
    }
*/

    /**
     * 工单漏传率统计表-日-导出
     *
     * @throws Exception
     */
    /*@PostMapping("/exportDay")
    public void exportDay(HttpServletResponse response, @RequestBody TOrderMissReportFromsDTO tOrderMissReportFromsDTO) throws Exception {
        List<TOrderMissReportFromsVO> tOrderMissReportFromsInfo = this.tOrderMissMonthService.getTOrderMissReportFromsInfo(tOrderMissReportFromsDTO);
        List<TOrderMissDayExportDTO> tOrderMissDayExportDTOS = new ArrayList<>();
        tOrderMissReportFromsInfo.forEach(tOrderMissReportFromsVO -> {
            TOrderMissDayExportDTO tOrderMissExportDTO = new TOrderMissDayExportDTO();
            tOrderMissExportDTO.setProvinceName(tOrderMissReportFromsVO.getProvinceName());

            tOrderMissExportDTO.setCpCount("0");

            tOrderMissExportDTO.setCpInstlMoveDetachCount("0");

            tOrderMissExportDTO.setLstfCount("0");

            tOrderMissExportDTO.setInstallCount("0");

            tOrderMissExportDTO.setMoveCount("0");

            tOrderMissExportDTO.setDetachCount("0");
            //获取工单类型集合
            List<TOrderDetailsDTO> tOrderMissDetailsDTOList = tOrderMissReportFromsVO.getDayCount();
            //根据工单类型集合分组
            if (CollUtil.isNotEmpty(tOrderMissDetailsDTOList)) {


                Map<String, List<TOrderDetailsDTO>> collect = tOrderMissDetailsDTOList.stream().collect(Collectors.groupingBy(TOrderDetailsDTO::getOrderType));

                //所有工单类型
                List<String> orderTypeAllList = this.tOrderMissMonthService.list().stream().map(TOrderMissMonth::getOrderType).distinct().collect(Collectors.toList());
                collect.forEach((orderType, tOrderMissDetailsDTOListMap) -> {
                    if ("move".equals(orderType)) {
                        orderTypeAllList.remove(orderType);
                        collect.get(orderType).forEach(tOrderMissDetailsDTO -> {
                            orderTypeAllList.remove(tOrderMissDetailsDTO.getOrderType());
                            tOrderMissExportDTO.setMoveCount(tOrderMissDetailsDTO.getCount().toString());
                        });
                    }
                    if ("cp_instl_move_detach".equals(orderType)) {
                        orderTypeAllList.remove(orderType);
                        collect.get(orderType).forEach(tOrderMissDetailsDTO -> {
                            orderTypeAllList.remove(tOrderMissDetailsDTO.getOrderType());
                            tOrderMissExportDTO.setCpInstlMoveDetachCount(tOrderMissDetailsDTO.getCount().toString());
                        });
                    }
                    if ("detach".equals(orderType)) {
                        orderTypeAllList.remove(orderType);
                        collect.get(orderType).forEach(tOrderMissDetailsDTO -> {
                            orderTypeAllList.remove(tOrderMissDetailsDTO.getOrderType());
                            tOrderMissExportDTO.setDetachCount(tOrderMissDetailsDTO.getCount().toString());
                        });
                    }
                    if ("install".equals(orderType)) {
                        orderTypeAllList.remove(orderType);
                        collect.get(orderType).forEach(tOrderMissDetailsDTO -> {
                            orderTypeAllList.remove(tOrderMissDetailsDTO.getOrderType());
                            tOrderMissExportDTO.setInstallCount(tOrderMissDetailsDTO.getCount().toString());
                        });
                    }
                    if ("cp".equals(orderType)) {
                        orderTypeAllList.remove(orderType);
                        collect.get(orderType).forEach(tOrderMissDetailsDTO -> {
                            orderTypeAllList.remove(tOrderMissDetailsDTO.getOrderType());
                            tOrderMissExportDTO.setCpCount(tOrderMissDetailsDTO.getCount().toString());
                        });
                    }
                    if ("lstf".equals(orderType)) {
                        orderTypeAllList.remove(orderType);
                        collect.get(orderType).forEach(tOrderMissDetailsDTO -> {
                            orderTypeAllList.remove(tOrderMissDetailsDTO.getOrderType());
                            tOrderMissExportDTO.setLstfCount(tOrderMissDetailsDTO.getCount().toString());
                        });
                    }
                });
            }
            tOrderMissDayExportDTOS.add(tOrderMissExportDTO);
        });

        final String fileName = "工单漏传统计-日";
        ExcelUtil.writeExcel(response, tOrderMissDayExportDTOS, fileName, "工单漏传统计-日", TOrderMissDayExportDTO.class);
    }*/
    @PostMapping("/installEquipment")
    public BaseResult<List<TOrderIndexInstallVo>> installEquipment(@RequestBody TOrderMonthDto tOrderYearDto) {
        return new BaseResult<>(this.tOrderIndexBusiMonthService.queryInstallEquipment(tOrderYearDto));
    }

    @PostMapping("/exportInstall")
    public void exportInstall(HttpServletResponse response, @RequestBody TOrderMonthDto tOrderYearDto) throws Exception {
        List<TOrderIndexInstallVo> tOrderIndexInstallVos = tOrderIndexBusiMonthService.exportInstall(tOrderYearDto);
        final String fileName = "分业务装机工单" + tOrderYearDto.getMonth();
        ExcelUtil.writeExcel(response, tOrderIndexInstallVos, fileName, "分业务装机工单" + tOrderYearDto.getMonth(), TOrderIndexInstallVo.class);
    }


    @PostMapping("/moveEquipment")
    public BaseResult<List<TOrderIndexMoveVo>> moveEquipment(@RequestBody TOrderMonthDto tOrderYearDto) {
        return new BaseResult<>(this.tOrderIndexBusiMonthService.queryMoveEquipment(tOrderYearDto));
    }

    @PostMapping("/exportMoves")
    public void exportMove(HttpServletResponse response, @RequestBody TOrderMonthDto tOrderYearDto) throws Exception {
        List<TOrderIndexMoveVo> tOrderIndexInstallVos = tOrderIndexBusiMonthService.exportMove(tOrderYearDto);
        final String fileName = "分业务移机工单" + tOrderYearDto.getMonth();
        ExcelUtil.writeExcel(response, tOrderIndexInstallVos, fileName, "分业务移机工单" + tOrderYearDto.getMonth(), TOrderIndexMoveVo.class);
    }

    @PostMapping("/dateList")
    public BaseResult<List<String>> dateList() {
        return new BaseResult<>(this.tOrderIndexBusiMonthService.dateList());
    }

}
